package com.it.flowable.common.vo.common;

import jdk.nashorn.internal.ir.annotations.Ignore;
import lombok.Data;
import org.apache.http.HttpStatus;

import java.io.Serializable;

@Data
public class CommonResult<T> implements Serializable {

    private Integer code; // 错误码
    private String msg; // 错误提示
    private T data; // 返回数据

    public static <T> CommonResult<T> error(CommonResult<?> result) {
        return error(result.getCode(), result.getMsg());
    }

    /**
     * 错误请求响应
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> error(Integer code, String msg) {
//        Assert.isTrue(code.intValue() == HttpStatus.SC_OK, "code必须是错误的，不能是200");
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(code);
        commonResult.setMsg(msg);

        return commonResult;
    }

    /**
     * 请求成功响应
     *
     * @param code
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> error(Integer code, String msg, T data) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        commonResult.setData(data);

        return commonResult;
    }

    /**
     * 请求成功响应
     *
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(String msg, T data) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(HttpStatus.SC_OK);
        commonResult.setMsg(msg);
        commonResult.setData(data);

        return commonResult;
    }

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(HttpStatus.SC_OK);
        commonResult.setData(data);
        return commonResult;
    }

    public static <T> CommonResult<T> success() {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(HttpStatus.SC_OK);
        commonResult.setMsg("成功");
        return commonResult;
    }

    /**
     * 通用请求响应
     *
     * @param code
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> commonRes(Integer code, String msg, T data) {
        CommonResult<T> commonResult = new CommonResult<>();
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        commonResult.setData(data);

        return commonResult;
    }

    /**
     * 方便判断是否成功
     * 忽略避免jackson序列化给前端
     *
     * @return
     */
    @Ignore
    public boolean isSuccess() {
        return this.code.equals(new Integer(HttpStatus.SC_OK));
    }

    @Ignore
    public boolean isError() {
        return !isSuccess();
    }

}
